Solving 10385 - Duathlon (Ternary search)
[andmenj-acm.git] / 11367 - Full tank? / comparación / 11367.cpp
blobe5254dc17693d46740e54a08d860dd97df7bd57c
1 /*
2 Time limit exceeded!
3 */
4 #include <iostream>
5 #include <vector>
6 #include <queue>
7 //#include <cassert>
9 using namespace std;
11 const int MAXN = 1000, MAXC = 100;
13 struct edge{
14 int i, g, w;
15 edge(){}
16 edge(int I, int G, int W) : i(I), g(G), w(W) {}
17 bool operator < (const edge &that) const {
18 return w > that.w;
22 int p[MAXN], d[MAXN][MAXC+1], n, visited, maxQSize;
23 vector<edge> g[MAXN];
26 int dijkstra(const int &start, const int &end, const int &c){
27 for (int i=0; i<n; ++i)
28 for (int j=0; j<=c; ++j)
29 d[i][j] = INT_MAX;
31 priority_queue<edge> q;
32 q.push(edge(start, 0, 0));
33 d[start][0] = 0;
35 while (q.size()){
37 maxQSize = max(maxQSize, (int)q.size());
39 edge u = q.top();
40 q.pop();
42 //printf("popped <%d, %d, %d>\n", u.i, u.g, u.w);
43 if (u.i == end){
44 return u.w;
46 if (d[u.i][u.g] < u.w) continue;
48 ++visited;
50 vector<edge> &v = g[u.i];
51 for (int j=0; j<v.size(); ++j){
52 int distance = v[j].w;
53 int neighbor = v[j].i;
54 for (int k = distance - u.g; k <= c + distance - u.g; ++k){
55 int new_gas = u.g - distance + k;
56 if (k >= 0 && 0 <= new_gas && u.g + k <= c ){
57 int w_extra = k*p[u.i];
58 //assert(w_extra >= 0);
59 if (u.w + w_extra < d[neighbor][new_gas]){
60 d[neighbor][new_gas] = u.w + w_extra;
61 q.push(edge(neighbor, new_gas, u.w + w_extra));
62 //printf(" pushed <%d, %d, %d>\n", neighbor, new_gas, u.w + w_extra);
68 return INT_MAX;
72 int main(){
73 int m;
74 scanf("%d %d", &n, &m);
75 for (int i=0; i<n; ++i){
76 scanf("%d", &p[i]);
79 while (m--){
80 int u, v, d;
81 scanf("%d %d %d", &u, &v, &d);
82 g[u].push_back(edge(v, 0, d));
83 g[v].push_back(edge(u, 0, d));
86 int q;
87 scanf("%d", &q);
88 while (q--){
89 int c, s, e;
90 scanf("%d %d %d", &c, &s, &e);
91 visited = 0;
92 maxQSize = 0;
93 int t = dijkstra(s, e, c);
94 if (t < INT_MAX){
95 printf("%d\n", t);
96 }else{
97 printf("impossible\n");
99 printf(" Visited %d states with maximum queue size of %d\n", visited, maxQSize);
101 return 0;